﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace SvgTool.Client.Common
{
    public class DelegateCommand<T> : ICommand where T: class  
    {

        public DelegateCommand(Func<T, bool> canExecuteFunc, Action<T> executeAction) { 
            this.ExecuteAction = executeAction;
            this.CanExecuteFunc = canExecuteFunc;
        }

        public DelegateCommand(Action<T> executeAction)
        {
            this.ExecuteAction = executeAction;
        }

        //https://www.cnblogs.com/seekdream/p/14815289.html
        public bool CanExecute(object? parameter)
        {
            if (this.CanExecuteFunc == null)
            {
                return true;
            }


            T param = parameter as T;
            if (param != null)
            {
                return this.CanExecuteFunc(param);
            }

            return false;

        }

        public event EventHandler? CanExecuteChanged;

        public void Execute(object? parameter)
        {

            T param = parameter as T;
            if (param != null)
            {
                this.ExecuteAction?.Invoke(param);
            }
        }

        public Action<T>? ExecuteAction { get; set; }
        public Func<T, bool>? CanExecuteFunc { get; set; }
    }

    public class DelegateCommand : ICommand
    {
        //https://www.cnblogs.com/seekdream/p/14815289.html
        public bool CanExecute(object? parameter)
        {
            if (this.CanExecuteFunc == null)
            {
                return true;
            }


            return this.CanExecuteFunc.Invoke(parameter);


        }

        public event EventHandler? CanExecuteChanged;

        public void Execute(object? parameter)
        {
            this.ExecuteAction?.Invoke();
        }

        public Action? ExecuteAction { get; set; }
        public Func<object, bool>? CanExecuteFunc { get; set; }
    }
}
